package com.ifengchao.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ifengchao.commonUtil.ServiceException;
import com.ifengchao.dao.iface.DealZzyPlanDao;
import com.ifengchao.pojo.ZzyPlanInfo;
import com.ifengchao.service.DealZzyPlanService;
import org.apache.commons.collections.map.HashedMap;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @title
 */
@Service
public class DealZzyPlanServiceImpl implements DealZzyPlanService {
    private static final Logger logger = Logger.getLogger(DealZzyPlanServiceImpl.class);

    @Autowired(required = true)
    private DealZzyPlanDao dealZzyPlanDao;

    @Override
    public void dealZzyPlan() throws ServiceException, Exception {
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK);
        SimpleDateFormat matter = new SimpleDateFormat("HH");
        String hour = matter.format(date);

        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        List<ZzyPlanInfo> list = dealZzyPlanDao.getZzyPlanList();
        for (int i = 0; i < list.size(); i++) {
            ZzyPlanInfo entity = list.get(i);
            //计划上下架状态：1、当为上架，判断是否为不限(做下架处理) 2、当为下架，判断是否要在时间段内(做上架处理)
            String status = entity.getStatus();
            String jsonStr = entity.getTimeSlot();
            JSONObject obj = JSONObject.parseObject(jsonStr);
            JSONArray array = obj.getJSONArray(String.valueOf(w));
            switch (status) {
                case "3":
                    // 1.1) 当为本星期设置为空&且为当前时间点，做下架处理
                    if (null == array) {
                        Map<String, Object> map = new HashedMap();
                        map.put("planId", entity.getPlanId());
                        map.put("status", "6");
                        mapList.add(map);
                    }
                    // 1.2) 当为本星期设置不为空&且当前时间点为空，做下架处理
                    else if (null != array && !array.contains(Integer.parseInt(hour))) {
                        Map<String, Object> map = new HashedMap();
                        map.put("planId", entity.getPlanId());
                        map.put("status", "6");
                        mapList.add(map);
                    }
                    break ;
                case "6":
                    // 2.1) 当为本星期且包含该时间点，做上架处理
                    if (null != array && array.contains(Integer.parseInt(hour))) {
                        Map<String, Object> map = new HashedMap();
                        map.put("planId", entity.getPlanId());
                        map.put("status", "3");
                        mapList.add(map);
                    }
                    break ;
            }
        }
        // 3.需要更新的计划list不为空时，执行上下架批量处理
        if(mapList.size()>0){
            logger.info("当前时间执行上下架的智子云计划 ："+mapList.toString());
            dealZzyPlanDao.updateShelfZzyPlan(mapList,500);
            dealZzyPlanDao.updateShelfZzyAdvertise(mapList,500);
        }
    }

    public static void main(String[] args) throws Exception {
//        dealZzyPlan();
        /**String jsonStr1 = "{\"1\":[18,19,20,21,22,23],\"2\":[18,19,20,21,22,23],\"3\":[18,19,20,21,22,23],\"4\":[18,19,20,21,22,23],\"5\":[18,19,20,21,22,23],\"6\":[18,19,20,21,22,23],\"7\":[18,19,20,21,22,23]}";
        String jsonStr2 = "{\"1\":[0,1,2,3,4,5],\"2\":[0,1,2,3,4,5],\"3\":[0,1,2,3,4,5],\"4\":[0,1,2,3,4,5],\"5\":[0,1,2,3,4,5],\"6\":[0,1,2,3,4,5],\"7\":[0,1,2,3,4,5]}";
        String jsonStr3 = "{\"1\":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23],\"3\":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]}";
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        List<ZzyPlanInfo> list = new ArrayList<ZzyPlanInfo>();

        ZzyPlanInfo entity = new ZzyPlanInfo();
        entity.setPlanId(Long.valueOf(2011));
        entity.setStatus("3");
        entity.setTimeSlot(jsonStr1);
        list.add(entity);

        entity = new ZzyPlanInfo();
        entity.setPlanId(Long.valueOf(2012));
        entity.setStatus("3");
        entity.setTimeSlot(jsonStr2);
        list.add(entity);

        entity = new ZzyPlanInfo();
        entity.setPlanId(Long.valueOf(2013));
        entity.setStatus("6");
        entity.setTimeSlot(jsonStr3);
        list.add(entity);

        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK);
        SimpleDateFormat matter = new SimpleDateFormat("HH");
        String hour = matter.format(date);
        for (int i = 0; i < list.size(); i++) {
            ZzyPlanInfo pojo = list.get(i);
            //计划上下架状态：1、当为上架，判断是否为不限(做下架处理) 2、当为下架，判断是否要在时间段内(做上架处理)
            String status = pojo.getStatus();
            String jsonStr = pojo.getTimeSlot();
            JSONObject obj = JSONObject.parseObject(jsonStr);
            JSONArray array = obj.getJSONArray(String.valueOf(w));
            switch (status) {
                case "3":
                    // 1.1) 当为本星期设置为空&且为当前时间点，做下架处理
                    if (null == array) {
                        Map<String, Object> map = new HashedMap();
                        map.put("planId", entity.getPlanId());
                        map.put("status", "6");
                        mapList.add(map);
                    }
                    // 1.2) 当为本星期设置不为空&且当前时间点为空，做下架处理
                    if (null != array && !array.contains(hour)) {
                        Map<String, Object> map = new HashedMap();
                        map.put("planId", entity.getPlanId());
                        map.put("status", "6");
                        mapList.add(map);
                    }
                case "6":
                    // 2.1) 当为本星期且包含该时间点，做上架处理
                    if (null != array && array.contains(hour)) {
                        Map<String, Object> map = new HashedMap();
                        map.put("planId", entity.getPlanId());
                        map.put("status", "3");
                        mapList.add(map);
                    }
                }
        }
            System.out.println("result: " +mapList.toString());**/
    }

}
